using System.Collections.Generic;
using UnityEngine;

public class CustomisationTabController : MonoBehaviour
{
	public PersonaModel displayModel;

	public List<CustomisationConfig.LookSetType> lookTypesToManage = new List<CustomisationConfig.LookSetType>();

	public CustomisationButton buttonPrefab;

	public UIGrid grid;

	public UIScrollView view;

	private List<CustomisationButton> customisationButtons = new List<CustomisationButton>();

	private bool isAligned;

	private List<CustomisationConfig.Look> _looksToManage;

	private Persona myPersona;

	private List<CustomisationConfig.Look> looksToManage
	{
		get
		{
			if (_looksToManage == null || _looksToManage.Count <= 0)
			{
				_looksToManage = GetLooks();
			}
			return _looksToManage;
		}
	}

	public void UpdateWithPersona(Persona persona)
	{
		myPersona = persona;
		ApplyInitialLooks();
		GenerateTab();
	}

	private List<CustomisationConfig.Look> GetLooks()
	{
		List<CustomisationConfig.Look> list = new List<CustomisationConfig.Look>();
		foreach (CustomisationConfig.LookSetType item in lookTypesToManage)
		{
			CustomisationConfig.LookSet lookSetForType = CustomisationConfig.instance.GetLookSetForType(item);
			foreach (CustomisationConfig.Look look in lookSetForType.looks)
			{
				if (myPersona != null && look.IsAvailable(myPersona))
				{
					list.Add(look);
				}
			}
		}
		return list;
	}

	private void OnEnable()
	{
		GenerateTab();
	}

	private void Start()
	{
		isAligned = false;
	}

	private void GenerateTab()
	{
		GeneratePage();
	}

	private void ApplyInitialLooks()
	{
		foreach (CustomisationConfig.LookSetType item in lookTypesToManage)
		{
			ApplyInitialLook(item);
		}
	}

	private void ApplyInitialLook(CustomisationConfig.LookSetType type)
	{
		CustomisationConfig.Look lookForType = myPersona.GetLookForType(type);
		if (lookForType != null)
		{
			lookForType.ApplyLook(displayModel, CustomisationConfig.ShaderUseType.MenuConfigPlayer);
		}
	}

	private void GeneratePage()
	{
		InitialiseButtonsForPage();
	}

	private void InitialiseButtonsForPage()
	{
		for (int i = 0; i < looksToManage.Count; i++)
		{
			if (i >= customisationButtons.Count)
			{
				CustomisationButton component = NGUITools.AddChild(grid.gameObject, buttonPrefab.gameObject).GetComponent<CustomisationButton>();
				component.gameObject.name = GGFormat.IndexName(i);
				customisationButtons.Add(component);
			}
			InitialiseButton(customisationButtons[i], looksToManage[i]);
		}
		for (int j = looksToManage.Count; j < customisationButtons.Count; j++)
		{
			InitialiseButton(customisationButtons[j], null);
		}
		grid.Reposition();
		isAligned = false;
	}

	private void InitialiseButton(CustomisationButton button, CustomisationConfig.Look look)
	{
		button.gameObject.SetActive(look != null);
		if (look != null)
		{
			button.Init(look, this);
			button.UpdateButtonSelectionHighlight(myPersona.IsLookActive(look));
		}
	}

	public void SelectLook(CustomisationConfig.Look look)
	{
		myPersona.ChangeLook(look);
		look.ApplyLook(displayModel, CustomisationConfig.ShaderUseType.MenuConfigPlayer);
		UpdateButtonHighlights();
	}

	private void UpdateButtonHighlights()
	{
		for (int i = 0; i < customisationButtons.Count; i++)
		{
			UpdateButtonHighlightIfActive(customisationButtons[i], i);
		}
	}

	private void UpdateButtonHighlightIfActive(CustomisationButton button, int currentButtonIndex)
	{
		if (currentButtonIndex < looksToManage.Count)
		{
			button.UpdateButtonSelectionHighlight(myPersona.IsLookActive(button.myLook));
		}
	}

	private void LateUpdate()
	{
		if (!isAligned && customisationButtons.Count > 0)
		{
			UITools.AlignToLeftOnScroll(customisationButtons[0].gameObject.GetComponent<UIWidget>(), view, 40f);
			isAligned = true;
		}
	}
}
